define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/classes/index' + location.search,
                    add_url: 'classes/classes/add',
                    edit_url: 'classes/classes/edit',
                    del_url: 'classes/classes/del',
                    multi_url: 'classes/classes/multi',
                    dragsort_url: '',
                    table: 'classes_classes',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: Config.students_id ? 'Classes.id' : 'id',
                sortName: Config.students_id ? 'Classes.id' : 'id',
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'classes_id',
                            title: __('Classes'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="classes/classes/index" data-field="title"',
                            operate: '=',
                            formatter: Table.api.formatter.search
                        },
                        {
                            field: 'admin_id',
                            title: __('School'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="classes/school/index" data-primary-key="admin_id" data-field="school_name"',
                            operate: '=',
                            searchable: Config.isSuperAdmin,
                            formatter: Table.api.formatter.search
                        },
                        {field: 'id', title: __('Id')},
                        {field: 'school.school_name', title: __('SchoolName'), searchable: false, visible: Config.isSuperAdmin && Config.authVersion == 2, formatter:function(value){ return value ?? '本部'; }},
                        {field: 'title', sortable: true, title: __('Title')},
                        {field: 'entry_year', title: __('Entry_year')},
                       /* {field: 'profile.realname', title: __('Realname')},
                        {field: 'profile.mobile', title: __('Mobile')},*/
                        {field: 'realname', title: __('Realname')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('审核通过'),"hidden":__('审核不通过')}, formatter: Table.api.formatter.status},


                        {
                            field: 'buttons',title: __('Teachersclasses'), table: table,
                            buttons: [{
                                name: 'teachersclasses',
                                text: __('View'),
                                operate: false,
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-eye',
                                url: 'classes/teachers/index?classes_id={ids}'
                            }],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',title: __('Students'), table: table,
                            buttons: [{
                                name: 'students',
                                text: __('View'),
                                operate: false,
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-eye',
                                url: 'classes/student/index?classes_id={ids}'
                            }],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                search: true,
                commonSearch: true
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});